package com.qf.controller;
/*
 *@Description:
 *@author:刘哥
 *@Date:19:48 2022/6/26
 */

import com.qf.service.impl.UserServiceImpl;
import com.qf.service.UserService;
import com.qf.utils.Base64Utils;
import com.qf.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:myxiaomi
 * author:刘哥
 * time:2022/6/26 19:48
 * description:
 */
@WebServlet("/activate")
public class ActivateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String emali = request.getParameter("e");
        String code = request.getParameter("c");
//        校验
        if (StringUtil.isEmpty(emali)||StringUtil.isEmpty(code)) {
            request.setAttribute("msg","激活失败");
            request.getRequestDispatcher("/message.jsp").forward(request,response);//转发
            return;
        }
        emali= Base64Utils.decode(emali);//解码
        code= Base64Utils.decode(code);//解码
//        激活
        UserService userSerice=new UserServiceImpl();
        try {
            userSerice.activa(emali,code);
            request.setAttribute("msg","激活成功");
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        } catch (Exception e) {
            request.setAttribute("msg","激活失败"+e.getMessage());
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
